package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public List<SetmealVO> list(Integer categoryId) {
        return setmealMapper.getByCategoryId(categoryId);
    }

    @Override
    public List<DishItemVO> dishItem(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmeaPagelDTO) {
        Page<SetmealVO> page = PageHelper.startPage(setmeaPagelDTO.getPage(), setmeaPagelDTO.getPageSize());
        List<SetmealVO> setmealVOS = setmealMapper.pageQuery(setmeaPagelDTO);
        return new PageResult(page.getTotal(), setmealVOS);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        setmealMapper.updateStatus(status, id);
    }

    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            setmealMapper.delete(id);
            setmealMapper.deleteSetmealDish(id);
        }
    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        return SetmealVO.builder()
                .id(setmeal.getId())
                .categoryId(setmeal.getCategoryId())
                .name(setmeal.getName())
                .price(setmeal.getPrice())
                .description(setmeal.getDescription())
                .image(setmeal.getImage())
                .status(setmeal.getStatus())
                .build();
    }

    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = Setmeal.builder()
                .categoryId(setmealDTO.getCategoryId())
                .name(setmealDTO.getName())
                .price(setmealDTO.getPrice())
                .description(setmealDTO.getDescription())
                .image(setmealDTO.getImage())
                .status(setmealDTO.getStatus())
                .build();
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealMapper.insertSetmealDish(setmealDish);
        }
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = Setmeal.builder()
                .id(setmealDTO.getId())
                .name(setmealDTO.getName())
                .categoryId(setmealDTO.getCategoryId())
                .price(setmealDTO.getPrice())
                .description(setmealDTO.getDescription())
                .image(setmealDTO.getImage())
                .status(setmealDTO.getStatus())
                .build();
        setmealMapper.update(setmeal);
        Long id = setmealDTO.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealMapper.deleteSetmealDish(id);
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
            setmealMapper.insertSetmealDish(setmealDish);
        }
    }
}
